﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using ICSharpCode.NRefactory;

namespace Commander.Core.Refactorings.Modifiers {

	public class JLongMessageChainModifier : NetWalkerCodeModifier {
		private int m_lineOfDefect;
		private string m_source;
		private string m_path;

		public JLongMessageChainModifier(XElement extra, IParser parser)
			: base(extra, parser) {
				m_source = extra.Attribute("src").Value;
				m_path = extra.Attribute("path").Value;
				m_lineOfDefect = GetLineNo(m_source);
		}

        public JLongMessageChainModifier(string source,string path)
        {
            m_path = path;
            StreamReader file = new StreamReader(m_path);
            m_source = file.ReadToEnd();
            Console.WriteLine("Here goes the source:\n"+m_source);
            m_lineOfDefect = GetLineNoTest(m_source);
            file.Close();
        }

		int GetLineNo(string source) {
			int startPos = source.IndexOf("≈");
			int endPos = source.IndexOf(")", startPos);
			string lineNoString = source.Substring(startPos + 1, endPos - startPos - 1);
			return Convert.ToInt32(lineNoString);
		}

        int GetLineNoTest(string source)
        {        
					
            int lineNo=-1,currentLine=1;
            String []stb=source.Split('\n');
            foreach (String str in stb)
            { 
                if(str.Contains("."))
                {
                    int count = str.Count(f => f == '.');
                    if (count > 2)
                        lineNo = currentLine;
                }
                if (lineNo > 0)
                    break;
                currentLine++;
            }
            return lineNo;
        }

		public int DefectLine {
			get {
				return this.m_lineOfDefect;
			}
		}

		public override string ApplyModification() {
			string line;
			int i = 1;

			string fileContent = null;

			if (File.Exists(m_path)) {
				StreamReader file = null;
				try {
					file = new StreamReader(m_path);
					while ((line = file.ReadLine()) != null) {
						if (i++ == DefectLine) {
							int firstEqual = line.IndexOf('=');
							int ln = firstEqual + 1;
							string unchanged = line.Substring(0, firstEqual + 1);
							string x = "testVar";
							int var = 0;
							int startIndex = firstEqual + 1;
							bool firstVar = false;
							string removeLongChain = null;
                            string dynVar = null;
							while (line[ln] != ';') {
								int splitposition = line.IndexOf(")", startIndex);
								string substr = line.Substring(startIndex, splitposition - startIndex + 1);
                                if (substr.Contains("get"))
                                {
                                    int getPos = (line.IndexOf("get", startIndex))+3;
                                    dynVar = line.Substring(getPos, line.IndexOf("(", getPos) - getPos);
                                }
                                else if (substr.Contains("is"))
                                {
                                    //int getPos = (line.IndexOf("get", startIndex)) + 3;
                                    dynVar = "boolean";
                                }
                                if (!firstVar)
                                {
									//removeLongChain = removeLongChain + "\t Object " + x + "" + var + " = " + substr + ";\n";
                                    removeLongChain = removeLongChain + "\t "+dynVar +" " + x + "" + var + " = " + substr + ";\n";
									firstVar = true;
								}
								else {
                                    removeLongChain = removeLongChain + "\t " + dynVar + " " + x + "" + var + " = " + x + (var - 1) + substr + ";\n";
								}
								var++;
								startIndex = splitposition + 1;
								ln = startIndex;
							}
							removeLongChain = removeLongChain + unchanged + x + (var - 1) + ";\n";
							fileContent = fileContent + removeLongChain + "\n";
						}
						else {
							fileContent = fileContent + line + "\n";
						}
					}
				}
				finally {
					if (file != null)
						file.Close();
				}
			}
			return fileContent;
		}
	}
}
